#include "DlgFind.h"
#include "mainwindow.h"

extern Translhextion mainwindow;

///////////////////////////////////////////////////////
CFindStringDlg::CFindStringDlg(HWND hWnd)
	: CDialog(hWnd,IDD)
{
	EventClick(IDC_MATCH_CASE,CFindStringDlg::OnClickMatchCase);
	EventClick(IDC_BYTECODE,CFindStringDlg::OnClickBytecode);
}

void CFindStringDlg::OnInitDialog()
{
	SetDlgItemText(GetWnd(),IDC_EDIT1,mainwindow.src_helper.GetSearchString());

	//SetFocus();

	// set match
	if(iFindDialogMatchCase)
	{
		CheckDlgButton(GetWnd(),IDC_MATCH_CASE,BST_CHECKED);
		EnableWindow(GetDlgItem(GetWnd(),IDC_BYTECODE),FALSE);
	}
	// set bytecode
	if(iFindDialogBytecode)
	{
		CheckDlgButton(GetWnd(),IDC_BYTECODE,BST_CHECKED);
		EnableWindow(GetDlgItem(GetWnd(),IDC_MATCH_CASE),FALSE);
	}
	// set direction
	if(iFindDialogDirection == -1) CheckDlgButton(GetWnd(),IDC_RADIO1, BST_CHECKED);
	else CheckDlgButton(GetWnd(),IDC_RADIO2, BST_CHECKED);
	// set encoding
	switch(iCharacterSettingSearch)
	{
	case ENCODING_BYTE:
		CheckDlgButton(GetWnd(),IDC_BYTE, BST_CHECKED);
		break;
	case ENCODING_WIDE:
		CheckDlgButton(GetWnd(),IDC_WIDE, BST_CHECKED);
		break;
	}
}

UINT CFindStringDlg::OnOK()
{
	// get match case
	iFindDialogMatchCase = IsDlgButtonChecked (GetWnd(), IDC_MATCH_CASE);
	// get bytecode
	iFindDialogBytecode = IsDlgButtonChecked (GetWnd(), IDC_BYTECODE);
	// get direction (1 up, -1 down)
	if (IsDlgButtonChecked (GetWnd(), IDC_RADIO1) == BST_CHECKED) iFindDialogDirection = -1;
	else iFindDialogDirection = 1;
	// get encoding
	if (IsDlgButtonChecked (GetWnd(), IDC_BYTE) == BST_CHECKED) iCharacterSettingSearch = ENCODING_BYTE;
	else if (IsDlgButtonChecked (GetWnd(), IDC_WIDE) == BST_CHECKED) iCharacterSettingSearch = ENCODING_WIDE;

	// setup search
	mainwindow.src_helper.SetSearchExpression(GetWindowText(IDC_EDIT1),iFindDialogMatchCase ? true : false,iCharacterSettingSearch,mainwindow.iBinaryMode);

	return DLGMESS_CLOSE;
}

void CFindStringDlg::OnClickBytecode()
{
	EnableWindow(GetDlgItem(GetWnd(),IDC_MATCH_CASE),IsDlgButtonChecked (GetWnd(), IDC_BYTECODE)==BST_CHECKED ? FALSE : TRUE );
}

void CFindStringDlg::OnClickMatchCase()
{
	EnableWindow(GetDlgItem(GetWnd(),IDC_BYTECODE),IsDlgButtonChecked (GetWnd(), IDC_MATCH_CASE)==BST_CHECKED ? FALSE : TRUE );
}

///////////////////////////////////////////////////////
CFindHexDlg::CFindHexDlg(HWND hWnd)
	: CDialog(hWnd,IDD)
{
}

void CFindHexDlg::OnInitDialog()
{
	SetDlgItemText(GetWnd(),IDC_EDIT1,mainwindow.src_helper.GetSearchHex());

	//SetFocus(GetWnd());

	CheckDlgButton (GetWnd(), IDC_RADIO2, BST_CHECKED);
	if(bSearchHex) CheckDlgButton(GetWnd(),IDC_RADIO5, BST_CHECKED);
	else CheckDlgButton(GetWnd(),IDC_RADIO4, BST_CHECKED);
}

UINT CFindHexDlg::OnOK()
{
	if (IsDlgButtonChecked (GetWnd(), IDC_RADIO1) == BST_CHECKED) iFindDialogDirection = -1;
	else iFindDialogDirection = 1;
	// set search data
	int result=mainwindow.src_helper.SetSearchHex(GetWindowText(IDC_EDIT1));

	switch(result)
	{
	case -1:
		MessageBox(LoadString(IDS_HEXSTRING_INVALID), LoadString(IDS_TITLE_FIND_ERROR), MB_OK | MB_ICONERROR);
		return DLGMESS_OPEN;
	case 0:
		MessageBox(LoadString(IDS_STRING_EMPTY), LoadString(IDS_TITLE_FIND_ERROR), MB_OK | MB_ICONERROR);
		return DLGMESS_OPEN;
	}

	return DLGMESS_CLOSE;
}

///////////////////////////////////////////////////////
CScanRelativeDlg::CScanRelativeDlg(HWND hWnd)
	: CDialog(hWnd,IDD)
{
	EventClick(IDC_SCAN,CScanRelativeDlg::OnScan);
	EventClick(IDC_GTABLE,CScanRelativeDlg::OnGenerateTable);
	EventClick(IDC_SAVE,CScanRelativeDlg::OnSave);
}

void CScanRelativeDlg::OnInitDialog()
{
	//SetFocus(GetDlgItem (GetWnd(), IDC_EDIT1));
	
	if (iFindDialogLastLength != 0 && bLastSearchType == 1)
		::SetWindowText (GetDlgItem (GetWnd(), IDC_EDIT1), mainwindow.src_helper.GetSearchRel());
	CheckDlgButton (GetWnd(), IDC_RADIO2, BST_CHECKED);
}

void CScanRelativeDlg::OnScan()
{
	if (IsDlgButtonChecked (GetWnd(), IDC_RADIO1) == BST_CHECKED)
		iFindDialogDirection = -1;
	else
		iFindDialogDirection = 1;

	progress1 = GetDlgItem(GetWnd(),IDC_SCANPROGRESS);
	bFileRelativeResults = true;
	SendMessage (GetDlgItem (GetWnd(), IDC_LIST1), LB_RESETCONTENT, 0, 0);
	std::vector<relativescandata> emp;
	vScanData = emp;
	GString s=GetWindowText(IDC_EDIT1);
	int result=s.GetLength();

	if(result == 1)
	{
		MessageBox(LoadString(IDS_NOTICE_RSEARC_MIN_LEN),LoadString(IDS_TITLE_NOTICE),MB_OK | MB_ICONINFORMATION);
		bFileRelativeResults = false;
		bRelativeSearchDone = false;
		
	}
	else if(result < 1)
	{
		
		MessageBox(_T("String to find not specified."),_T("Search Relative error:"), MB_OK | MB_ICONERROR );
		bFileRelativeResults = false;
		bRelativeSearchDone = false;
		
	}
	else
	{
		SetCursor (LoadCursor (NULL, IDC_WAIT));
		//create_relative_search_string();
		mainwindow.src_helper.CreateRelativeSearchString(s);
		iFindDialogLastLength = result;
		mainwindow.scan_relative_helper(result);
		bLastSearchType = 1;
		if(bRelativeSearchDone)
		{
			MessageBox (LoadString(IDS_NOTICE_RSEARCH_NO_FOUND), LoadString(IDS_TITLE_NOTICE), MB_OK | MB_ICONINFORMATION);
			bFileRelativeResults = false;
			bRelativeSearchDone = false;
			bLastSearchType = 1;
		}
		else
		{
			vScanData.push_back(mainwindow.guess_table_values(mainwindow.iCurrentByte));
			int k = 0;
			int l = 20;
			SendMessage (GetDlgItem (GetWnd(), IDC_LIST1), LB_ADDSTRING, 0, (LPARAM) (makelength(vScanData[k].search,l) + makelength(stringvalue(vScanData[k].start),l) + vScanData[k].listitem).c_str());
			SendMessage (GetDlgItem (GetWnd(), IDC_LIST1), LB_SETCURSEL, k, 0);
			while(!bRelativeSearchDone)
			{
				if (iFindDialogDirection == 1)
				{
					mainwindow.findnext_relative();
				}
				else
				{
					mainwindow.findprev_relative();
				}
				vScanData.push_back(mainwindow.guess_table_values(mainwindow.iCurrentByte));
				k = vScanData.size() - 1;
				SendMessage (GetDlgItem (GetWnd(), IDC_LIST1), LB_ADDSTRING, 0, (LPARAM) (makelength(vScanData[k].search,l) + makelength(stringvalue(vScanData[k].start),l) + vScanData[k].listitem).c_str());
				SendMessage (GetDlgItem (GetWnd(), IDC_LIST1), LB_SETCURSEL, k, 0);
			}
			SetCursor (LoadCursor (NULL, IDC_ARROW));
			mainwindow.adjust_view_for_selection();
			mainwindow.repaint();
			
			SendMessage (GetDlgItem (GetWnd(), IDC_LIST1), LB_SETCURSEL, 0, 0);
			bFileRelativeResults = false;
			bRelativeSearchDone = false;
			bLastSearchType = 1;
		}
	}
	ResetProgress(progress1);
	iIncrementStep = 0;
	iProgressPos = 0;
	progress1 = NULL;
}

void CScanRelativeDlg::OnGenerateTable()
{
	if(vScanData.size() > 0)
	{
		int index = SendMessage (GetDlgItem (GetWnd(), IDC_LIST1), LB_GETCURSEL, 0, 0);

		CFileDialog dlg;
		dlg.Create(GetWnd(),LoadString(IDS_FILTER_TABLE),LoadString(IDS_FILTER_TABLE_TITLE),OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,false);

		if (dlg.DoModal())
		{
			fstream fs;
			sFileName=FixFilename(dlg.GetFileName(),_T(".tbl"));
			fs.open(sFileName.c_str(),ios::out);
			if(!fs.fail())
			{
				if(vScanData[index].A != _T("??"))
				{
					int left = char_hex(vScanData[index].A);
					int right = 'A';
					int k = 0;
					for(k = 0;k<26;k++)
					{
						//fs << hex_char(left) << L"=" << (unsigned char) right << endl;
						left++;
						right++;
					}
				}
				if(vScanData[index].a != _T("??"))
				{
					int left = char_hex(vScanData[index].a);
					int right = 'a';
					int k = 0;
					for(k = 0;k<26;k++)
					{
						//fs << hex_char(left) << "=" << (unsigned char) right << endl;
						left++;
						right++;
					}
				}
				if(vScanData[index].n != _T("??"))
				{
					int left = char_hex(vScanData[index].n);
					int right = '1';
					int k = 0;
					for(k = 0;k<9;k++)
					{
						//fs << hex_char(left) << "=" << (unsigned char) right << endl;
						left++;
						right++;
					}
				}
				fs.close();
				MessageBox ((sFileName + _T(" sucessfully generated.")).c_str(), _T("Table Generation Complete"), MB_OK);
			}
			else
			{
				MessageBox (_T("Could not save file."), _T("Relative Scan error:"), MB_OK | MB_ICONERROR);
				fs.close();
			}
		}
	}
	else
		MessageBox(LoadString(IDS_NOTICE_TABLE_NO_RESULTS),LoadString(IDS_TITLE_NOTICE),MB_OK | MB_ICONINFORMATION);
}

void CScanRelativeDlg::OnSave()
{
	if(vScanData.size() > 0)
	{
		TCHAR cFileName[_MAX_PATH];
		TCHAR szTitleName[_MAX_FNAME + _MAX_EXT];
		OPENFILENAME ofn;
		szTitleName[0] =cFileName[0] = '\0';
		ofn.lStructSize = sizeof (OPENFILENAME);
		ofn.hwndOwner = GetWnd();
		ofn.hInstance = NULL;
		ofn.lpstrFilter = _T("Text Files (*.TXT)\0*.TXT\0All files (*.*)\0*.*\0\0");
		ofn.lpstrCustomFilter = NULL;
		ofn.nMaxCustFilter = 0;
		ofn.nFilterIndex = 0;
		ofn.lpstrFile = cFileName;
		ofn.nMaxFile = _MAX_PATH;
		ofn.lpstrFileTitle = szTitleName;
		ofn.nMaxFileTitle = _MAX_FNAME + _MAX_EXT;
		ofn.lpstrInitialDir = NULL;
		ofn.lpstrTitle = _T("Save Search Results As");
		ofn.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
		ofn.nFileOffset = 0;
		ofn.nFileExtension = 0;
		ofn.lpstrDefExt = NULL;
		ofn.lCustData = 0L;
		ofn.lpfnHook = NULL;
		ofn.lpTemplateName = NULL;
		if (GetSaveFileName (&ofn))
		{
			fstream fs;
			//int cur = ztrlen(cFileName) - 4;
			sFileName=FixFilename(cFileName,_T(".TXT"));
			fs.open(sFileName.c_str(),ios::out);
			if(!fs.fail())
			{
				GString message;
				mainwindow.save_relative_results(fs);
				fs.close();
				message.Format(LoadString(IDS_NOTICE_RESULT_SAVED),sFileName.c_str());
				MessageBox (message, LoadString(IDS_TITLE_NOTICE), MB_OK | MB_ICONINFORMATION);
			}
			else
			{
				MessageBox(_T("Could not save file."),_T("Relative Scan error:"), MB_OK | MB_ICONERROR);
				fs.close();
			}
		}
	}
	else
	{
		MessageBox(LoadString(IDS_NOTICE_NO_RESULT_SAVE),LoadString(IDS_TITLE_NOTICE),MB_OK | MB_ICONINFORMATION);
	}
}

///////////////////////////////////////////////////////
CRelativeValueDlg::CRelativeValueDlg(HWND hWnd)
	: CDialog(hWnd,IDD)
{
	EventClick(IDC_ADD,CRelativeValueDlg::OnAdd);
	EventClick(IDC_REMOVE,CRelativeValueDlg::OnRemove);
	EventClick(IDC_SKIP,CRelativeValueDlg::OnSkip);
	EventClick(IDC_RESET,CRelativeValueDlg::OnReset);
	EventClick(IDC_SCAN,CRelativeValueDlg::OnScan);
	EventClick(IDC_SAVE,CRelativeValueDlg::OnSave);
	EventClick(IDC_RADIO3,CRelativeValueDlg::OnDecimal);
	EventClick(IDC_RADIO4,CRelativeValueDlg::OnHex);
	EventClick(IDC_RADIO5,CRelativeValueDlg::OnCharacter);
}

void CRelativeValueDlg::OnInitDialog()
{
	//SetFocus(GetDlgItem (GetWnd(), IDC_RADIO3));

	CheckDlgButton (GetWnd(), IDC_RADIO3, BST_CHECKED);
	SendMessage (GetDlgItem (GetWnd(), IDC_EDIT1), EM_LIMITTEXT, 3, 0);
	vValueList.clear();// = viEmpty;
	iLastRelativeValue = -1;
	SendMessage (GetDlgItem (GetWnd(), IDC_COMBO1), CB_INSERTSTRING, 0, (LPARAM) _T("Up"));
	SendMessage (GetDlgItem (GetWnd(), IDC_COMBO1), CB_INSERTSTRING, 0, (LPARAM) _T("Down"));
	SendMessage (GetDlgItem (GetWnd(), IDC_COMBO1), CB_SETCURSEL, 0,0);
}

void CRelativeValueDlg::OnAdd()
{
	int valuemode = -1;
	int value = -1;
	int change = 0;
	_tstring sign;
	relation a;
	int buflen = SendMessage (GetDlgItem (GetWnd(), IDC_EDIT1), EM_GETLIMITTEXT, 0, 0) + 1;
	TCHAR *cBuf = new TCHAR[buflen];
	int result = GetDlgItemText (GetWnd(), IDC_EDIT1, cBuf, buflen);
	if(IsDlgButtonChecked(GetWnd(),IDC_RADIO3))
	{
		valuemode = 0;
		if(intvalue((_tstring)cBuf) > 255 || intvalue((_tstring)cBuf) < 0)
		{
			MessageBox(LoadString(IDS_NOTICE_DEC_OUTOFRANGE),LoadString(IDS_TITLE_NOTICE),MB_OK | MB_ICONINFORMATION);
			return;
		}
		value = intvalue((_tstring) cBuf);
	}
	else if(IsDlgButtonChecked(GetWnd(),IDC_RADIO4))
	{
		valuemode = 1;
		value = char_hex((_tstring) cBuf);
	}
	else
	{
		valuemode = 2;
		value = cBuf[0];
	}
	if(vValueList.size() == 0)
	{
		iLastRelativeValue = value;
		SendMessage (GetDlgItem (GetWnd(), IDC_LIST1), LB_ADDSTRING, 0, (LPARAM) (stringvalue(value)).c_str());
	}
	else
	{
		GString message;
		change = value - iLastRelativeValue;
		iLastRelativeValue = value;
		if(change < 0)
		{
			sign = _T("-");
		}
		else if(change > 0)
		{
			sign = _T("+");
		}
		else
		{
			sign = _T("");
		}
		message.Format(_T("%d (%s%d)"),value,sign.c_str(),value);
		SendMessage (GetDlgItem (GetWnd(), IDC_LIST1), LB_ADDSTRING, 0, (LPARAM)(LPCTSTR)message);
	}
	vValueList.push_back(value);
	SendMessage (GetDlgItem (GetWnd(), IDC_EDIT1), EM_SETSEL, 0, -1);
	SendMessage (GetDlgItem (GetWnd(), IDC_EDIT1), WM_CLEAR, 0, 0);
	delete [] cBuf;
}

void CRelativeValueDlg::OnRemove()
{
	int selected = SendMessage (GetDlgItem (GetWnd(), IDC_LIST1), LB_GETCURSEL, 0, 0);
	if(selected == LB_ERR)
	{
		MessageBox(LoadString(IDS_NOTICE_SEL_REMOVE_VALUE),LoadString(IDS_TITLE_NOTICE),MB_OK | MB_ICONINFORMATION);
		return;
	}	
	//vValueList.remove(selected);
	vValueList.erase(vValueList.begin()+selected);
	SendMessage (GetDlgItem (GetWnd(), IDC_LIST1), LB_DELETESTRING, selected, 0);
}

void CRelativeValueDlg::OnSkip()
{
	if(vValueList.size() == 0)
		MessageBox(LoadString(IDS_NOTICE_CANT_SKIP_FIRST),LoadString(IDS_TITLE_NOTICE),MB_OK | MB_ICONINFORMATION);
	else
	{
		vValueList.push_back(-1);
		SendMessage (GetDlgItem (GetWnd(), IDC_LIST1), LB_ADDSTRING, 0, (LPARAM) _T("Skipped Byte"));
	}
}

void CRelativeValueDlg::OnReset()
{
	vValueList = viEmpty;
	SendMessage (GetDlgItem (GetWnd(), IDC_LIST1), LB_RESETCONTENT, 0, 0);
	SendMessage (GetDlgItem (GetWnd(), IDC_LIST2), LB_RESETCONTENT, 0, 0);
}

void CRelativeValueDlg::OnScan()
{
	if(vValueList.size() < 2)
	{
		MessageBox(LoadString(IDS_NOTICE_NEED_2_VALUES),LoadString(IDS_TITLE_NOTICE),MB_OK | MB_ICONINFORMATION);
		return;
	}
	if(vValueList[vValueList.size()-1] == -1)
	{
		MessageBox(LoadString(IDS_NOTICE_CANT_SKIP_LAST),LoadString(IDS_TITLE_NOTICE),MB_OK | MB_ICONINFORMATION);
		return;
	}
	relation a;
	if(SendMessage (GetDlgItem (GetWnd(), IDC_COMBO1), CB_GETCURSEL, 0, 0) == 0)
	{
		iFindDialogDirection = 1;
	}
	else
	{
		iFindDialogDirection = -1;
	}
	std::vector<relation> emp2;
	vRelativeOffsets = emp2;
	int k = 0;
	int m = 0;
	for(k = 0;k<(int)vValueList.size()-1;k++)
	{
		if(vValueList[k+1] == -1)
		{
		}
		else
		{
			a.relate = m;
			a.related = k + 1;
			a.offset = vValueList[k+1] - vValueList[m];
			a.op = "=";
			vRelativeOffsets.push_back(a);
			m = k;
			m++;
		}
	}
	progress1 = GetDlgItem(GetWnd(),IDC_SCANPROGRESS);
	bFileRelativeResults = true;
	SendMessage (GetDlgItem (GetWnd(), IDC_LIST2), LB_RESETCONTENT, 0, 0);
	std::vector<relativescandata> emp;
	vScanData = emp;
	int result = vValueList.size();
	sRelSearchString = _T("");
	for(k = 0;k<(int)vValueList.size();k++)
	{
		sRelSearchString += _T("*");
	}
	mainwindow.scan_relative_helper(result);
	bLastSearchType = 1;
	if(bRelativeSearchDone)
	{
		MessageBox (LoadString(IDS_NOTICE_RSEARCH_NO_FOUND), LoadString(IDS_TITLE_NOTICE), MB_OK | MB_ICONINFORMATION);
		bFileRelativeResults = false;
		bRelativeSearchDone = false;
		bLastSearchType = 1;
	}
	else
	{
		vScanData.push_back(mainwindow.guess_table_values(mainwindow.iCurrentByte));
		int k = 0;
		SendMessage (GetDlgItem (GetWnd(), IDC_LIST2), LB_ADDSTRING, 0, (LPARAM) stringvalue(vScanData[k].start).c_str());
		SendMessage (GetDlgItem (GetWnd(), IDC_LIST2), LB_SETCURSEL, k, 0);
		while(!bRelativeSearchDone)
		{
			if (iFindDialogDirection == 1)
			{
				mainwindow.findnext_relative();
			}
			else
			{
				mainwindow.findprev_relative();
			}
			vScanData.push_back(mainwindow.guess_table_values(mainwindow.iCurrentByte));
			k = vScanData.size() - 1;
			SendMessage (GetDlgItem (GetWnd(), IDC_LIST2), LB_ADDSTRING, 0, (LPARAM) stringvalue(vScanData[k].start).c_str());
			SendMessage (GetDlgItem (GetWnd(), IDC_LIST2), LB_SETCURSEL, k, 0);
		}
		SetCursor (LoadCursor (NULL, IDC_ARROW));
		mainwindow.adjust_view_for_selection();
		mainwindow.repaint();
		SendMessage (GetDlgItem (GetWnd(), IDC_LIST2), LB_SETCURSEL, 0, 0);
		
		bFileRelativeResults = false;
		bRelativeSearchDone = false;
		bLastSearchType = 1;
	}
	ResetProgress(progress1);
	iIncrementStep = 0;
	iProgressPos = 0;
	progress1 = NULL;
}

void CRelativeValueDlg::OnSave()
{
	if(vScanData.size() > 0)
	{
		TCHAR cFileName[_MAX_PATH];
		TCHAR szTitleName[_MAX_FNAME + _MAX_EXT];
		OPENFILENAME ofn;
		szTitleName[0] =cFileName[0] = '\0';
		ofn.lStructSize = sizeof (OPENFILENAME);
		ofn.hwndOwner = GetWnd();
		ofn.hInstance = NULL;
		ofn.lpstrFilter = _T("Text Files (*.TXT)\0*.TXT\0\0");
		ofn.lpstrCustomFilter = NULL;
		ofn.nMaxCustFilter = 0;
		ofn.nFilterIndex = 0;
		ofn.lpstrFile = cFileName;
		ofn.nMaxFile = _MAX_PATH;
		ofn.lpstrFileTitle = szTitleName;
		ofn.nMaxFileTitle = _MAX_FNAME + _MAX_EXT;
		ofn.lpstrInitialDir = NULL;
		ofn.lpstrTitle = _T("Save Search Results As");
		ofn.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
		ofn.nFileOffset = 0;
		ofn.nFileExtension = 0;
		ofn.lpstrDefExt = NULL;
		ofn.lCustData = 0L;
		ofn.lpfnHook = NULL;
		ofn.lpTemplateName = NULL;
		if (GetSaveFileName (&ofn))
		{
			fstream fs;
			//int cur = ztrlen(cFileName) - 4;
			//if(uppercase(((string) cFileName).substr(((string) cFileName).length()-4,4)) == ".TXT")
			//{
			//	sFileName = (string) cFileName;
			//}
			//else
			//{
			//	sFileName = (string) ztrcat(cFileName,".txt");
			//}
			sFileName=FixFilename(cFileName,_T(".TXT"));
			fs.open(sFileName.c_str(),ios::out);
			if(!fs.fail())
			{
				GString message;
				mainwindow.save_relative_results(fs);
				fs.close();
				message.Format(LoadString(IDS_NOTICE_RESULT_SAVED),sFileName.c_str());
				MessageBox (message, LoadString(IDS_TITLE_NOTICE), MB_OK | MB_ICONINFORMATION);
			}
			else
			{
				MessageBox (_T("Could not save file."), _T("Relative Scan error:"), MB_OK | MB_ICONERROR);
				fs.close();
			}
		}
	}
	else
	{
		MessageBox(LoadString(IDS_NOTICE_NO_RESULT_SAVE),LoadString(IDS_TITLE_NOTICE),MB_OK | MB_ICONINFORMATION);
	}
}

void CRelativeValueDlg::OnDecimal()
{
	SendMessage (GetDlgItem (GetWnd(), IDC_EDIT1), EM_SETSEL, 0, -1);
	SendMessage (GetDlgItem (GetWnd(), IDC_EDIT1), WM_CLEAR, 0, 0);
	SendMessage (GetDlgItem (GetWnd(), IDC_EDIT1), EM_LIMITTEXT, 3, 0);
	//SetFocus(GetDlgItem(GetWnd(),IDC_EDIT1));
}

void CRelativeValueDlg::OnHex()
{
	SendMessage (GetDlgItem (GetWnd(), IDC_EDIT1), EM_SETSEL, 0, -1);
	SendMessage (GetDlgItem (GetWnd(), IDC_EDIT1), WM_CLEAR, 0, 0);
	SendMessage (GetDlgItem (GetWnd(), IDC_EDIT1), EM_LIMITTEXT, 2, 0);
	//SetFocus(GetDlgItem(GetWnd(),IDC_EDIT1));
}

void CRelativeValueDlg::OnCharacter()
{
	SendMessage (GetDlgItem (GetWnd(), IDC_EDIT1), EM_SETSEL, 0, -1);
	SendMessage (GetDlgItem (GetWnd(), IDC_EDIT1), WM_CLEAR, 0, 0);
	SendMessage (GetDlgItem (GetWnd(), IDC_EDIT1), EM_LIMITTEXT, 1, 0);
	//SetFocus(GetDlgItem(GetWnd(),IDC_EDIT1));
}

